Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use rust's own bool type in abstraction crate #61

Merged
merged 2 commits into from
Nov 5, 2021

Conversation

vkkoskie
Copy link
Contributor

@vkkoskie vkkoskie commented Nov 5, 2021

The Bbool type was translating between the FFI's CK_BBOOL and rust's own bool, by providing a backing intermediate type. But the storage for a bool is guaranteed to be large enough for the job, and the point of conversion from byte back to bool is already structured to check for validity.

@vkkoskie
Copy link
Contributor Author

vkkoskie commented Nov 5, 2021

Ref for the data layout, which is also present in a comment in the commit: https://doc.rust-lang.org/reference/type-layout.html#primitive-data-layout

The Bbool type was translating between the FFI's CK_BBOOL and
rust's own bool, by providing a backing intermediate type. But
the storage for a bool is guaranteed to be large enough for the
job, and the point of conversion from byte back to bool is
already structured to check for validity.

Signed-off-by: Keith Koskie <[email protected]>
@@ -617,7 +617,7 @@ impl Attribute {
| Attribute::Verify(_)
| Attribute::VerifyRecover(_)
| Attribute::Wrap(_)
| Attribute::WrapWithTrusted(_) => 1,
| Attribute::WrapWithTrusted(_) => std::mem::size_of::<bool>(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extreme corner case -> CK_BBOOL is defined as an unsigned char. What if the architecture defines a char to not be 8-bits (e.g. not POSIX compliant).

Googling this question turns up two TI DSPs that if someone implemented a cryptoki token on I'd question their sanity.

I think this is fine, but just wanted to pose the question.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. It may be possible at an architectural level, but the standard itself excludes that possibility by explicitly saying "unsigned 8-bit value".

At this particular location, the type being sized is exactly the type that the raw pointer points to. So if, say, CK_BBOOL somehow gets bigger because of a weird backing c_uchar, the ptr and len calls still match each other. And the provider should return CKR_BUFFER_TOO_SMALL or some other error code.

@mike-boquard
Copy link
Collaborator

This looks fine by me. And since Rust's true is equivalent to CK_TRUE this should work.

@vkkoskie
Copy link
Contributor Author

vkkoskie commented Nov 5, 2021

This looks fine by me. And since Rust's true is equivalent to CK_TRUE this should work.

Funny enough, there is some ambiguity here. CK_TRUE is defined to 1, but the range of valid values is much more lax:

In Cryptoki, the CK_BBOOL data type is a Boolean type that can be true or false. A zero value means
false, and a nonzero value means true.

Should the range error condition on the u8->bool be removed?

@mike-boquard
Copy link
Collaborator

In Cryptoki, the CK_BBOOL data type is a Boolean type that can be true or false. A zero value means false, and a nonzero value means true.

That's why I love this spec 🙄

Should the range error condition on the u8->bool be removed?

Yeah probably to be as spec-compliant as possible (which I have droned on about in the past).

auto-merge was automatically disabled November 5, 2021 03:17

Head branch was pushed to by a user without write access

@vkkoskie
Copy link
Contributor Author

vkkoskie commented Nov 5, 2021

That's why I love this spec 🙄

If it's any consolation, I don't like it either.

Copy link
Member

@ionut-arm ionut-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks! 👍🏻

@mike-boquard mike-boquard merged commit d5e1992 into parallaxsecond:main Nov 5, 2021
@vkkoskie vkkoskie deleted the use-primitive-bool branch November 6, 2021 00:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants